package com.qf.service.impl;

import com.qf.dao.UserDao;
import com.qf.dao.impl.UserDaoImpl;
import com.qf.domain.Cart;
import com.qf.domain.Goods;
import com.qf.domain.User;
import com.qf.service.GoodsService;
import com.qf.service.UserService;
import com.qf.utils.MD5Utils;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    @Override
    public boolean checkUserName(String username) {
        User user = userDao.selectByUserName(username);
        if (user != null) {
            return true;//用户已存在 1 不能注册
        }
        return false;//用户不存在 0 可以注册
    }

    @Override
    public void register(User user) {
        //密码加密
        user.setPassword(MD5Utils.md5(user.getPassword()));
        userDao.insert(user);
    }

    @Override
    public User login(String username, String password) {
        //加密
        password = MD5Utils.md5(password);
        //查找用户
        User user = userDao.selectByUserName(username);
        //判断用户是否存在
        if (user == null) {
            throw new RuntimeException("用户名不存在");
        }
        //比对密码
        if (!user.getPassword().equalsIgnoreCase(password)) {
            throw new RuntimeException("密码错误");
        }
        //是否激活
        if (user.getFlag() != 1) {
            throw new RuntimeException("用户未激活或激活已过期");
        }
        return user;
    }

    @Override
    public List<User> findUserInfo(String toString, List<Object> params, boolean flag) {
        return userDao.selectAll(toString,params,flag);
    }

    @Override
    public void removeId(int did) {
        userDao.delete(did);
    }

    @Override
    public User findById(Integer uid) {
        return userDao.selectById(uid);
    }

}
